<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

class KomaWidgetListBox extends KomaWidget
{   
    function __construct($name, $value = null, $selected = '', $validators = null, $size = 1, $multiple = false, $cssclass = 'koma-ListBox')
    {
        $this->createProperties(array(
                                      'Selected' => ($selected == null) ? '' : $selected,
                                      'Multiple' => '',
                                      'Options' => '',
                                      'Size' => $size));
        
        if($multiple)
        {
            $this->Multiple = 'multiple="multiple" ';
            $name = $name.'[]';
        }
        
        parent::__construct('<select class="'.$cssclass.'" id="'.$name.'" '.$this->Multiple.'name="'.$name.'" size="'.$size.'"></select>');
        
        if($validators != null) KomaValidatorManager::getInstance()->initializeValidator($name, $validators);
        
        if($value != null) $this->addItem($value);
    }
    
    function addItem($item, $name = '')
    {
        if(is_array($item))
        {
            foreach($item as $value => $text)
            {
                $this->Options .= '<option value="'.$value.'">'.$text.'</option>';
            }
        }
        else
        {
            $name != '' ? $this->Options .= '<option value="'.$name.'">'.$item.'</option>' : '<option value="'.$item.'">'.$item.'</option>';
        }
        
        $this->Options = str_replace('<option value="'.$this->Selected.'">',
                                      '<option selected="selected" value="'.$this->Selected.'">',
                                      $this->Options);
        $this->UI = str_replace('</select>', $this->Options.'</select>', $this->UI);
    }
}
?>